<?php
	namespace Home\Model;
	use Think\Model;

	class SysUserModel extends Model
	{
		//规定表名
		protected $trueTableName = 't_user';

		// 防止越权curd
		public function get_canlook_user($userid,$province_name)
		{	
			// province_name  作查询条件待用---
			//1.查底下权限的角色
			$role_id = $this->where(array("id"=>$userid))->getField("role_id");
			$pid = D("SysRole")->where(array("id"=>$role_id))->getField("parent_id");
			// 2.查该用户对应的省份的id 根据省份id去查用户
			// $arr1 = D("UserProvince")->where(array("user_id"=>$userid))->field("province_id")->select();
			$arr1 = D("UserProvince")->field("province_id")->select();
			foreach ($arr1 as $key => $value) {
				$arr4[] = $value['province_id'];
			}
			// dump($arr4);exit; //1 2 3
			$map1['province_id'] = array("in",$arr4);
			$arr2 = D("UserProvince")->where($map1)->field("user_id")->select();
			// dump($arr2);
			foreach ($arr2 as $key => $value) {
				$a[] = $value['user_id'];
			}
			$a = array_unique($a);
			// arr3为该省的用户id
			// 3.拼接查询用户的条件
			$map['parent_id']  = array('egt',$pid);
			$res1 = D("SysRole")->where($map)->select();
			foreach ($res1 as $key => $value) {
				$arr4[] = $value['id'];
			}
			$arr4 = array_unique($arr4);
			$where['role_id'] = array("in",$arr4);
			$where['id'] = array("in",$a);
			$res = $this->where($where)->field("id,username,role_id,status,recent_time,ip")->select();
			// dump($res);
			foreach ($res as $key1 => $value) {	
				$arr5[] = D("SysRole")->where(array("id"=>$value['role_id']))->getField("role_name");
				$arr6 = D("UserProvince")->where(array("user_id"=>$value['id']))->field("province_id")->select();
				// dump($arr6);exit;   arr6为province的id 
				$arr7 = [];
				foreach ($arr6 as $key => $value) {
					$where1['id'] = $value['province_id'];
					$arr7[] = D("Province")->where($where1)->field("id,province")->select();
				}
				// dump($arr7);exit();  //arr7为省份数组
				$a1 = '';
				foreach ($arr7 as $kk => $vv) {			
					$a1 .= $vv[0]['province']." ";
					$res[$key1]['province_name'] = $a1;
				}
				foreach ($arr5 as $key => $v) {
					$res[$key1]['role_name'] = $v;		
				}
			}
			// dump($province_name);exit;
			// --------待用-------
			$province_name == ''?$res:$res = $this->get_all($province_name,$res);
			// dump($res);exit;
			return $res?$res:'';
		}


		// ---省份搜索待用----
		public function get_all($province_name,$res1)
		{
			// 对查取用户进行处理  获取到省份名字
			$province_name1 = explode(",",$province_name);   //province_name1  为数组
			// dump($province_name1);exit;
			foreach ($province_name1 as $key => $value) {
				$res[] = $this->get_all1($value,$res1);		// 获取到$value
			}	
			// dump($res);exit;
			// 对$res进行数据处理
			foreach ($res as $key => $value) {
				foreach ($value as $k => $v) {
					$data[] = $v;
				}
			}
			$data = array_unique($data,SORT_REGULAR);
			// var_dump($value);exit;
			return $data;
		}

		public function get_all1($value1,$res1)
		{
			// $data = [];
			foreach ($res1 as $key => $value) {
				$province_names = explode(" ",trim($value['province_name']," "));  //获取到省份名
				if(in_array($value1,$province_names)){
					$data[$key] = $value;
				}
				// dump($data);exit;
			}
			// dump($data);exit;
			return $data;
		}

		public function get_canuse_role($userid)
		{
			// 得到可用角色信息
			$role_id = $this->where(array("id"=>$userid))->getField("role_id");
			$pid = D("SysRole")->where(array("id"=>$role_id))->getField("parent_id");
			// 得到pid及其以下的角色
			$map['parent_id']  = array('gt',$pid);
			$res = D("SysRole")->where($map)->select();
			// dump($res);
			foreach ($res as $key => $value) {
				$arr1[$key]['id'] = $value['id'];
				$arr1[$key]['text'] = $value['role_name'];
			}
			return $arr1?$arr1:'';
		}

		public function get_canuse_province($userid)
		{
			// 得到可用省份信息
			// $arr2 = D("UserProvince")->where(array("user_id"=>$userid))->select();
			// $arr2 = D("UserProvince")->select();
			// // dump($arr2);exit();
			// foreach ($arr2 as $key => $value) {
			// 	// dump($value);exit;
			// 	$a[] = $value['province_id'];
			// }
			// $map['id'] = array("in",$a);
			// $arr3 = D("Province")->where($map)->select();
			// foreach ($arr3 as $key => $value) {
			// 	$arr4[$key]['id'] = $value['id'];
			// 	$arr4[$key]['text'] = $value['province'];
			// }
			// return $arr4?$arr4:'';
			$res = D("Province")->select();
			foreach ($res as $key => $value) {
				$res[$key]['id'] = $value['id'];
				$res[$key]['text'] = $value['province'];
			}
			return $res?$res:'';
		}

		public function add_user($username,$password,$status,$province_name,$role_name)
		{
			$data['username'] = $username;
			$data['password'] = $password;
			$data['status'] = $status;
			$data['role_id'] = D("SysRole")->where(array("role_name"=>$role_name))->getField("id");
			$data['create_time'] = time();
			$data['recent_time'] = time();
			$last_user_id = $this->add($data);
			$province_arr = explode(',', $province_name);
			foreach ($province_arr as $key => $value) {
				$prov_ids[] = D("Province")->where(array("province"=>$value))->getField("id");	
			}			
			foreach ($prov_ids as $key => $value) {
				$data['user_id'] = $last_user_id;
				$data['province_id'] = $value;
				$res = D("UserProvince")->add($data);
			}
			return ($res && $last_user_id)?true:false;
		}

		public function now_user($userid,$act_userid)
		{
			$role_id = $this->where(array("id"=>$userid))->getField("role_id");
			$del_roleid = $this->where(array("id"=>$act_userid))->getField("role_id");
			$pid = D("SysRole")->where(array("id"=>$role_id))->getField("parent_id");
			$del_pid = D("SysRole")->where(array("id"=>$del_roleid))->getField("parent_id");
			// 查出对应的省份是否一致
			$arr1 = D("UserProvince")->where(array("user_id"=>$userid))->select();
			$arr2 = D("UserProvince")->where(array("user_id"=>$del_roleid))->select();
			foreach ($arr1 as $key => $value) {
				$arr3[] = $value['province_id'];
			}
			foreach ($arr2 as $key => $value) {
				$arr4[] = $value['province_id'];
			}
			$arr5 = array_diff($arr4,$arr3);
			// dump($del_pid); //2
			// dump($pid); //0
			// dump($arr5);exit;//empty
  			if(($del_pid <= $pid) || (!empty($arr5))){
  				// dump("ahaha");exit;
				return false;
			}else{
				return true;
			}
		}

		// 删除该省份和低级别的用户
		public function delete_user($userid,$del_userid)
		{
			$res = $this->now_user($userid,$del_userid);
  			if($res){
  				// 开始删除操作  用户表+用户省份中间表
				$res1 = D("SysUser")->where(array("id"=>$del_userid))->delete();
				$res2 = D("UserProvince")->where(array("user_id"=>$del_userid))->delete();
				return ($res1 && $res2)?$code = 1:$code = 0;
			}else{
				return $code = 2;
			}
		}

		// 查询出修改用户的信息
		public function edit_select($userid,$edit_userid)
		{
			$res = $this->now_user($userid,$edit_userid);
			// dump($res);exit;
			if($res){
				// dump($edit_userid);exit;
				// 开始查询出该用户信息+省份信息+角色信息
				$res1 = $this->where(array("id"=>$edit_userid))->field("id,username,status")->select();
				$res2 = D("UserProvince")->where(array("user_id"=>$edit_userid))->select();
				foreach ($res2 as $key => $value) {
					$a[] = $value['province_id'];
				}
				// dump($a);exit;
				if($a){
					$map['id'] = array("in",$a);
					$b = D("Province")->where($map)->select();
				}
				// dump($b);exit;
				foreach ($b as $key => $value) {
					$c[] = $value['province'];
				}
				$c = implode(",",$c);
				$role_id = D("SysUser")->where(array("id"=>$edit_userid))->getField("role_id");
				if($role_id){
					$res3 = D("SysRole")->where(array("id"=>$role_id))->field("id,role_name")->select();
				}
				// dump($res3);exit;
				if($res3){
					foreach ($res3 as $key => $value) {
						$d[] = $value['role_name'];
					}
					$d = implode(",",$d);
				}	
				foreach ($res1 as $key => $value) {
					// dump($d);
					$res1[$key]['role_name'] = $d;
					$res1[$key]['province_name'] = $c;
				}
				// dump($res1);exit;
				return $res1?$res1:"";
			}else{
				return $code = 2;
			}
		}

		public function edit($param)
		{
			// 获取到数据逐个修改
			$data['username'] = $param['username'];
			$data['status'] = $param['status'];
			$data['recent_time'] = time();
			$role_name = explode(",",$param['role_name']);
			$province_name = explode(",",$param['province_name']);
			// dump($role_name[0]);exit;
			$role_id = D("SysRole")->where(array("role_name"=>$role_name[0]))->getField("id"); //得到角色id
			$data['role_id'] = $role_id;
			// 修改用户表
			$res1 = $this->where(array("id"=>$param['id']))->save($data);
			foreach ($province_name as $key => $value) {
				$ids[] = D("Province")->where(array("province"=>$value))->getField("id");
			}	
			// dump($ids);exit;   //省份ids
			$res2 = D("UserProvince")->where(array("user_id"=>$param['id']))->delete();
			foreach ($ids as $key => $value) {
				// dump($value);
				$data1['province_id'] = $value;
				$data1['user_id'] = $param['id'];
				$res3[] = D("UserProvince")->add($data1);
			}
			return ($res1 && $res3)?true:false;
		}

		public function modify_pass($userid,$oldpassword,$newpassword)
		{
			$password = $this->where(array("id"=>$userid))->getField("password");
			// dump($password);
			// dump($oldpassword);exit;
			if($password != $oldpassword){
				return $code = 0;  //密码不一致
			}else{
				$data['password'] = $newpassword;
				$res = $this->where(array("id"=>$userid))->save($data);  
				// dump($res);exit;
				return $res?$code = 1:$code = 2;  // 1 success  2 fail 
			}
		}

	}